package com.jiuyou.clientController.footprint;

import com.alibaba.fastjson.JSONObject;
import com.jiuyou.clientController.base.ClientBaseController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.DicConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.footprint.domain.Footprint;
import com.ruoyi.footprint.service.IFootprintService;
import com.ruoyi.product.domain.StoreProduct;
import com.ruoyi.product.service.IStoreProductService;
import com.ruoyi.shopping.domain.MallProduct;
import com.ruoyi.shopping.service.IMallProductService;
import com.ruoyi.store.domain.Store;
import com.ruoyi.store.service.IStoreService;
import com.ruoyi.storeclassify.domain.Storeclassify;
import com.ruoyi.storeclassify.service.IStoreclassifyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 足迹
 *
 * @author jiuyou
 */
@Api(tags = {"C端足迹相关"})
@RestController
@RequestMapping("/client/footPrint")
public class FootPrintClientController extends ClientBaseController
{
    private static final Logger log = LoggerFactory.getLogger(FootPrintClientController.class);

    @Autowired
    private IFootprintService footprintService;
    @Autowired
    private IStoreService storeService;
    @Autowired
    private IStoreclassifyService storeclassifyService;
    @Autowired
    private IStoreProductService storeProductService;
    @Autowired
    private IMallProductService mallProductService;

    /**
     * 查询足迹记录列表
     */
    @ApiOperation(value = "查询足迹记录列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前第几页",required = true,dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize",value = "每页几条数据",required = true,dataType = "Integer"),
            @ApiImplicitParam(name = "footprintType",value = "收藏类型，门店传store,商品传product",required = true,dataType = "String"),
    })
    @PostMapping("/list")
    public AjaxResult list(String footprintType,Integer pageNum,Integer pageSize)
    {
        Long loginMemberId=getLoginMemberId();
        if(null==loginMemberId){
            return AjaxResult.error("登录状态异常");
        }
        Footprint footprint=new Footprint();
        footprint.setFootprintType(footprintType);
        footprint.setMemberId(loginMemberId);
        startPage();
        List<Footprint> list = footprintService.selectFootprintList(footprint);
        TableDataInfo tableDataInfo =getDataTable(list);
        list= (List<Footprint>) tableDataInfo.getRows();
        List<JSONObject> resultList=new ArrayList<JSONObject>();
        for (Footprint ft:list) {
            JSONObject jso=new JSONObject();
            String footprintTypeObj= ft.getFootprintType();
            jso.put("footprintType",footprintTypeObj);
            if(DicConstants.COLLECT_TYPE_STORE.getCode().equals(footprintTypeObj ))  {
                    //门店
                Long storeId=ft.getBusId();
                Store store= storeService.selectStoreById(storeId);
                if(null==store){
                    jso=null;
                    continue;
                }
                jso.put("storeImage",store.getStoreImage());
                jso.put("storeName",store.getStoreName());
                jso.put("consumption",store.getConsumption());
                jso.put("storeLevel",store.getStoreLevel());
                Storeclassify storeclassify= storeclassifyService.selectStoreclassifyById(store.getStoreClassify());
                if(null==storeclassify){
                    jso=null;
                    continue;
                }
                jso.put("storeClassify", storeclassify.getClassifyName());

            }
            if(DicConstants.COLLECT_TYPE_PRODUCT.getCode().equals(footprintTypeObj ))  {
                //商品
                Long productId=ft.getBusId();
                StoreProduct storeProduct= storeProductService.selectStoreProductById(productId);
                if(null==storeProduct){
                    jso=null;
                    continue;
                }
                jso.put("productName", storeProduct.getProductName());
                jso.put("productImage",storeProduct.getProductImage());
                jso.put("price",storeProduct.getPrice());
                jso.put("storePrice",storeProduct.getStorePrice());
                jso.put("busId",storeProduct.getId());
                jso.put("storeName",storeProduct.getStoreName());
                jso.put("id",ft.getId());
            }if(DicConstants.COLLECT_TYPE_MALL_PRODUCT.getCode().equals(footprintTypeObj ))  {
                //商品
                Long productId=ft.getBusId();
                MallProduct product = mallProductService.selectMallProductById(productId);
                if(null==product){
                    jso=null;
                    continue;
                }
                jso.put("productName", product.getProductName());
                jso.put("productImage",product.getProductImage());
                jso.put("price",product.getCurrentPrice());
                jso.put("busId",product.getId());
                jso.put("id",ft.getId());
            }
            if(null!=jso){
                resultList.add(jso);
            }

        }
        tableDataInfo.setRows(resultList);
        return AjaxResult.success(tableDataInfo);
    }


    /**
     * 新增足迹记录
     */
    @ApiOperation(value = "添加收藏")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "busId",value = "根据足迹类型不同传门店id或者商品id",required = true,dataType = "Long"),
            @ApiImplicitParam(name = "footprintType",value = "足迹类型，门店传store,商品传product",required = true,dataType = "String"),
    })
    @PostMapping("/add")
    public AjaxResult add(String footprintType,Long busId,String classifyId)
    {

        Long loginMemberId=getLoginMemberId();
        if(null==loginMemberId){
            return AjaxResult.error("登录状态异常");
        }
        Footprint footprint=new Footprint();
        footprint.setFootprintType(footprintType);
        footprint.setBusId(busId);
        footprint.setMemberId(loginMemberId);
        footprint.setClassifyId(classifyId);
        return toAjax(footprintService.insertFootprint(footprint));
    }


    /**
     * 删除足迹记录
     */
    @ApiOperation(value = "删除足迹记录，支持数组id删除")
    @ApiImplicitParam(name = "ids",value = "要删除的收藏记录id多个使用英文逗号分割，例如1,2,3,4",required = true)
    @PostMapping("/del/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        Long loginMemberId=getLoginMemberId();
        if(null==loginMemberId){
            return AjaxResult.error("登录状态异常");
        }
        return toAjax(footprintService.deleteFootprintByIds(ids,loginMemberId));
    }
}
